'use strict';

app.controller('forgetCapitalPswCtl', function($scope,$interval,$localStorage,$http,rest,$compile,$location,ngDialog,user,$state,$window,uiLoad,$rootScope,$ocLazyLoad,tipService) {

    $scope.params = {
       
       
    };
    $scope.user = {
        Phone: user.get('phone'),
        PasswordType : 3
    };
    $scope.authError = null;
    $scope.signinShow = false;
    $scope.sms = {
        totalSecond: 60,
        sending: false,
        btnText: "发送验证码",
        promise: ""
    };

    $scope.signToggle = function () {
        $scope.signinShow = !$scope.signinShow;
    }

    $scope.getCode = function () {
        if (!$scope.user.Phone) {
            return false;
        }

        // delete $localStorage.user;

        countingStart();

        rest.post({
            methodId: 4003,
            data: {
                Phone: $scope.user.Phone
            },
            success: function (result) {
                console.log(result);
            }
        })

        return false;
    };

    var countingStart = function () {
        // first of all, cancel all counting
        $interval.cancel($scope.sms.promise);

        var counting = $scope.sms.totalSecond;
        $scope.sms.sending = true;

        $scope.sms.btnText = "重新获取(" + (counting--) + ")";

        $scope.sms.promise = $interval(function () {
            $scope.sms.btnText = "重新获取(" + (counting--) + ")";
            if (counting < 0) {
                $scope.sms.btnText = "获取验证码";
                $scope.sms.sending = false;
                counting = $scope.sms.totalSecond;
                $interval.cancel($scope.sms.promise);
            }
        }, 1000);
    };

    $scope.saveData = function() {
        if ($scope.user.smsCode != $scope.user.smsCode) {
            tipService.show("body", {
                message: "验证码输入错误",
                notice: "danger"
            }, 3);
            return;
        }
        rest.post({
            methodId: '5018',
            data: $scope.user,
            success: function(res) {
                if (res.code == 0) {
                    tipService.show("body", {
                        message: "密码重置成功"
                    }, 3);
                    console.log($scope.user);
                } else {
                    tipService.show("body", {
                        message: "密码重置失败!",
                        notice: "danger"
                    }, 3);
                }
            },
            error: function() {

            }
        })
    }
    $scope.backList = function() {
        $scope.params = {};
        $location.path('/app/houseOwner/capitalPsw');
    }





});